<?php
    require_once '../funciones.php';
    $con        = con_caweb();
    $soporte    =$_SESSION['caweb']['soporte'];
    $comercial  =$_SESSION['caweb']['comercial'];
    $base       =$_SESSION['caweb']['basededatos'];
    $idabonos   = $_REQUEST['idabono'];
    $versolosincancelar = $_REQUEST['versolosincancelar'];
    $versolovigentes    = $_REQUEST['versolovigentes'];
	$page = $_REQUEST['page'];

	$condicion="";
	if ($versolosincancelar=='Si') $condicion.=" AND fac.estadocartera <> 'C'";
	if ($versolosincancelar=='No') $condicion.="";
	if ($versolovigentes=='Si') $condicion.=" AND fac.estado = 'V'";
	if ($versolovigentes=='No') $condicion.="";

	// get how many rows we want to have into the grid - rowNum parameter in the grid
	$limit = $_REQUEST['rows'];

	// get index row - i.e. user click to sort. At first time sortname parameter -
	// after that the index from colModel
	$sidx = $_REQUEST['sidx'];

	// sorting order - at first time sortorder
	$sord = $_REQUEST['sord'];

	// if we not pass at first time index use the first column for the index or what you want
	if(!$sidx) $sidx =1;

        $sql = "select cliente from cabeceraabonosId where idabonos = $idabonos";
        $result = mysql_query($sql, $con) or die("Error SQL 2".mysql_error().$sql);
        $cliente = mysql_result($result, 0,"cliente");

	// calculate the number of rows for the query. We need this for paging the result
        $sql = "SELECT count(det.facturasistema) as count FROM detallesabonosId det
                    WHERE det.idabonos =$idabonos";
        $tmsql = $sql;
        $result = mysql_query($sql,$con) or die("Error SQL 3".mysql_error().$sql);
	$row = mysql_fetch_array($result,MYSQL_ASSOC);
	$count = $row['count'];

	// calculate the total pages for the query
	if( $count > 0 ) $total_pages = ceil($count/$limit);
	else $total_pages = 0;

	// if for some reasons the requested page is greater than the total
	// set the requested page to total page
	if ($page > $total_pages) $page=$total_pages;

	// calculate the starting position of the rows
	$start = $limit*$page - $limit;

	// if for some reasons start position is negative set it to 0
	// typical case is that the user type 0 for the requested page
	if($start <0) $start = 0;

	// the actual query for the grid data
        $sql    = "SELECT det.facturasistema,det.rteFuente as retefuente,det.rteIva as reteiva,det.rteIca as reteica,det.otros as otros,det.cuadre as cuadre,det.ajuste,det.observaciones as observacioncartera,estadocartera,'0' as prueba FROM detallesabonosId det
                    WHERE det.idabonos =$idabonos ORDER BY prueba,$sidx $sord LIMIT $start , $limit";
        $facturas = mysql_query($sql,$con) or die("Error SQL 2".mysql_error().$sql);

	// we should set the appropriate header information
	if ( stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml") ) {
				  header("Content-type: application/xhtml+xml;charset=utf-8");
	} else {
			  header("Content-type: text/xml;charset=utf-8");
	}
	echo "<?xml version='1.0' encoding='utf-8'?>";
	echo "<rows>";
	echo "<page>".$page."</page>";
	echo "<total>".$total_pages."</total>";
	echo "<records>".$count."</records>";
	// be sure to put text data in CDATA
	while($row = mysql_fetch_array($facturas,MYSQL_ASSOC)) {
                $sql = "select ajuste,factura,fechafactura,tipo,base,iva,total,estado,estadocartera,juridico,fechapago,valorpagado,retefuente,reteiva,reteica,otros,cuadre,observacioncartera from cabeceras_fac where facturasistema = ".$row[facturasistema];
                $result = mysql_query($sql, $con) or die("Error SQL 34".mysql_error().$sql);
                //validaciones si los valores hacen match
                $match = '';
                if(!(mysql_result($result, 0,"ajuste") == $row[ajuste])) $match = 'Si';
                if(!((mysql_result($result, 0,"retefuente") - $row[retefuente]) == 0)) $match = 'Si';
                if(!((mysql_result($result, 0,"reteiva") - $row[reteiva]) == 0)) $match = 'Si';
                if(!((mysql_result($result, 0,"reteica") - $row[reteica]) == 0)) $match = 'Si';
                if(!((mysql_result($result, 0,"otros") - $row[otros]) == 0)) $match = 'Si';
                if(!((mysql_result($result, 0,"cuadre") - $row[cuadre]) == 0)) $match = 'Si';
                //termina validaciones
                if($match == "") $match = 'No';
		echo "<row id='". $row[facturasistema]."'>";                
                echo "<cell>". $match."</cell>";
		echo "<cell>". mysql_result($result, 0,"ajuste")."</cell>";
		echo "<cell>". mysql_result($result, 0,"factura")."</cell>";
		echo "<cell>". mysql_result($result, 0,"tipo")."</cell>";
		echo "<cell>". mysql_result($result, 0,"fechafactura")."</cell>";
		echo "<cell>". number_format(mysql_result($result, 0,"base"),0, ',', '.')."</cell>";
		echo "<cell>". number_format(mysql_result($result, 0,"iva"),0, ',', '.')."</cell>";
		echo "<cell>". number_format(mysql_result($result, 0,"total"),0, ',', '.')."</cell>";
		echo "<cell>". mysql_result($result, 0,"estado")."</cell>";
		if (mysql_result($result, 0,"estadocartera") == 'A') echo "<cell>Abonada</cell>";
		else echo "<cell>". mysql_result($result, 0,"estadocartera") ."</cell>";
		echo "<cell>". mysql_result($result, 0,"juridico")."</cell>";
		echo "<cell>". mysql_result($result, 0,"fechapago")."</cell>";
		echo "<cell>". mysql_result($result, 0,"valorpagado")."</cell>";
		echo "<cell>". mysql_result($result, 0,"retefuente")."</cell>";
		echo "<cell>". mysql_result($result, 0,"reteiva")."</cell>";
		echo "<cell>". mysql_result($result, 0,"reteica")."</cell>";
		echo "<cell>". mysql_result($result, 0,"otros")."</cell>";
		echo "<cell>". mysql_result($result, 0,"cuadre")."</cell>";
		echo "<cell>". utf8_encode(htmlspecialchars(mysql_result($result, 0,"observacioncartera")))."</cell>";
		echo "</row>";
	}
	echo "</rows>";

?>
